﻿/**
<code>
  <author name="Zealic" email="zealic(at)gmail.com" type="Creator"/>
</code>
**/
using System;
using System.Runtime.InteropServices;


namespace Zealib.Win32
{
    /// <summary>
    /// OS version
    /// </summary>
    public sealed class OSVersion
    {
        #region PInvoke define
        [StructLayout(LayoutKind.Sequential)]
        private struct _OSVERSIONINFOEX
        {
            public int dwOSVersionInfoSize;
            public int dwMajorVersion;
            public int dwMinorVersion;
            public int dwBuildNumber;
            public PlatformID dwPlatformId;
            [MarshalAs(UnmanagedType.LPTStr, SizeConst = 128)]
            public string szCSDVersion;
            public ushort wServicePackMajor;
            public ushort wServicePackMinor;
            public ProductSuites wSuiteMask;
            public ProductType wProductType;
            public byte wReserved;
        }

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool GetVersionEx(ref _OSVERSIONINFOEX lpVersionInfo);

        #endregion

        private _OSVERSIONINFOEX m_VersionInfo;

        private OSVersion(_OSVERSIONINFOEX versionInfo)
        {
            m_VersionInfo = versionInfo;
        }

        /// <summary>
        /// Get OSVersion instance
        /// </summary>
        /// <returns></returns>
        public static OSVersion GetOSVersion()
        {
            _OSVERSIONINFOEX vi = new _OSVERSIONINFOEX();
            GetVersionEx(ref vi);
            return new OSVersion(vi);
        }

        public PlatformID PlatformID
        {
            get { return m_VersionInfo.dwPlatformId; }
        }

        public int Minor
        {
            get { return m_VersionInfo.dwMinorVersion; }
        }

        public int Major
        {
            get { return m_VersionInfo.dwMajorVersion; }
        }

        public int BuildNumber
        {
            get { return m_VersionInfo.dwBuildNumber; }
        }

        public ProductSuites ProductSuite
        {
            get { return m_VersionInfo.wSuiteMask; }
        }

        public ProductType ProductType
        {
            get { return m_VersionInfo.wProductType; }
        }

        public string ServicePack
        {
            get { return m_VersionInfo.szCSDVersion; }
        }

    }
}
